﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

namespace BW.Mes.WinForms.BasicData
{
    public partial class ProductSelectForm : DevExpress.XtraEditors.XtraForm
    {
        public List<ProductForm.ProductModel> SelectedProducts { get; private set; } = new List<ProductForm.ProductModel>();
        private GridControl gridControl1;
        private GridView gridView1;
        private SimpleButton btnOK;
        private SimpleButton btnCancel;
        private List<BomSparepartViewModel> _bomSpareparts;

        public ProductSelectForm(List<ProductForm.ProductModel> allProducts, List<BomSparepartViewModel> bomSpareparts = null)
        {
            InitializeComponent();
            InitCustomUI();
            _bomSpareparts = bomSpareparts ?? new List<BomSparepartViewModel>();
            gridControl1.DataSource = allProducts;
        }

        private void InitCustomUI()
        {
            this.Width = 800;
            this.Height = 500;
            gridControl1 = new GridControl();
            gridView1 = new GridView(gridControl1);
            gridControl1.Dock = DockStyle.Fill;
            gridControl1.MainView = gridView1;
            gridControl1.ViewCollection.Add(gridView1);
            this.Controls.Add(gridControl1);

            gridView1.OptionsSelection.MultiSelect = true;
            gridView1.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CheckBoxRowSelect;
            gridView1.BestFitColumns();
            // 设置BomSparepartId列标题

            var panelBottom = new Panel
            {
                Dock = DockStyle.Bottom,
                Height = 50
            };
            btnOK = new SimpleButton { Text = "确定", Left = 550, Top = 10, Width = 100 };
            btnCancel = new SimpleButton { Text = "取消", Left = 670, Top = 10, Width = 100 };
            btnOK.Click += BtnOK_Click;
            btnCancel.Click += (s, e) => { this.DialogResult = DialogResult.Cancel; this.Close(); };
            panelBottom.Controls.Add(btnOK);
            panelBottom.Controls.Add(btnCancel);
            this.Controls.Add(panelBottom);
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            SelectedProducts = gridView1.GetSelectedRows()
                .Select(rowHandle => gridView1.GetRow(rowHandle) as ProductForm.ProductModel)
                .Where(p => p != null)
                .ToList();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void ProductSelectForm_Load(object sender, EventArgs e)
        {

        }
    }
}